package com.suian.spring.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

@Component
public class BeanListApplicationRunner implements ApplicationRunner {
    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        System.out.println("--------------------------------------------------");
        System.out.println("-------------------All beans----------------------");
        System.out.println("--------------------------------------------------");

        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanName : beanDefinitionNames) {
            if (applicationContext.getBean(beanName).getClass().getName().contains("suian")) {
                System.out.println(beanName + " -> " + applicationContext.getBean(beanName).getClass().getName());
            }

        }
        System.out.println("--------------------------------------------------");
        System.out.println("-------------------垃圾回收器 ----------------------");
        System.out.println("--------------------------------------------------");

        List<GarbageCollectorMXBean> list = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean bean : list) {
            System.out.println(bean.getName());
        }

        System.out.println("--------------------------------------------------");
        System.out.println("------------------系统启动成功----------------------");
        System.out.println("--------------------------------------------------");



    }
}
